package com.hczt.xhminiapp.db.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 红创-马海强
 * @date 2019-03-13 11:33
 * @description
 */
@Data
@Entity
@Table(name = "t_sys_permission")
public class SysPermission implements Serializable {

    private static final long serialVersionUID = 2852968184401273864L;
    @Id
    @GenericGenerator(name = "idGenerator", strategy = "uuid")
    @GeneratedValue(generator = "idGenerator")
    @Column(name = "permission_id", length = 40)
    private String  permissionId;

    /**
     * 父编号
     */
    private String  parentId = "0";

    /**
     * 资源明后才能
     */
    private String  title;

    /**
     * 资源路径
     */
    private String  url;

    /**
     * 权限字符串,
     * menu例子：role:*
     * button例子：role:create,role:update,role:delete,role:view
     */
    private String permission;

    /**
     * 顺序
     */
    private Integer sort;

    /**
     * 图标
     */
    private String  icon;

    /**
     * //资源类型，[menu|button]
     */
    private String type;

    private Boolean enabled = Boolean.TRUE;

    private Boolean deleted = Boolean.FALSE;

    @Column(updatable = false)
    private Date    createTime;

    @Transient
    private Boolean selected;

    @Transient
    private List<SysPermission> children = new LinkedList<>();

    /**
     * 角色 -- 权限关系：多对多关系;
     */
    @JsonIgnore
    @ManyToMany
    @JoinTable(name="SysRolePermission",joinColumns={@JoinColumn(name="permissionId")},inverseJoinColumns={@JoinColumn(name="roleId")})
    private List<SysRole> roles;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SysPermission sysPermission = (SysPermission) o;
        return permissionId.equals(sysPermission.permissionId);
    }

    @Override
    public int hashCode() {
        return permissionId.hashCode();
    }
}
